package xj.graph2d.app;

public abstract class Runner implements Runnable, Cloneable {
  
  protected Runner() {}

  protected boolean done = false;

  public Object clone() throws CloneNotSupportedException { 
    return super.clone();
  }

  public void setDone(boolean b) { 
    done = b; 
  }

  public boolean isSuspended() {  
    return suspended;
  }

  public void suspend() {  
    suspended = true; 
  }

  public void resume() {  
    suspended = false; 
  }

  public void stopThread() { 
    //System.out.println("xj.graph2d.Runner.stopThread():" + toString() + " " + main.getCurrentProjectItemName());
  
    if (runnerThread != null && 
        runnerThread.isAlive()) { 
      setDone(true);
      Thread moribund = runnerThread;
      runnerThread = null;
      moribund.interrupt();
    } else {
      runnerThread = null;
    }
  }

  public void interruptThread() { 
    if (runnerThread != null && 
        runnerThread.isAlive()) { 
      runnerThread.interrupt();
    }
  }

  public void startThread() { 
    startThread(Thread.MAX_PRIORITY); 
  }

  public void startThread(int priority) { 
    //System.out.println("xj.graph2d.Runner.startThread():" + toString() + " " + main.getCurrentProjectItemName());
  
    if (isThreadAlive()) { 
      stopThread();
    }
  
    done = false;
    runnerThread = new Thread(this);
    runnerThread.setPriority(3); 
    runnerThread.start();     
  }

  public boolean isThreadAlive() { 
    return (runnerThread != null && 
            runnerThread.isAlive());
  }

  protected boolean suspended = false;
  protected Thread runnerThread = null;

}
